package com.ruoyi.flow.auth.system;

import com.ruoyi.flow.auth.menu.MenuReader;
import com.ruoyi.flow.common.tools.filewatch.FileListener;
import com.ruoyi.flow.common.tools.filewatch.FileWatcher;
import com.ruoyi.flow.common.utils.XmlUtils;
import com.ruoyi.flow.auth.system.System;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FilenameFilter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @Author: wtian
 * @Description:
 * @Date: Created in 9:00 2018/8/10
 * @Version: 1.0
 */
public class SystemReader {
    private static final Logger LOGGER = LoggerFactory.getLogger(MenuReader.class);
    public static final String CONFIG_PATH = "/conf/admin/systems.xml";
    private List<com.ruoyi.flow.auth.system.System> systems;

    private SystemReader() {
        read();
        //检测文件被修改后，重新加载
        FileWatcher watcher = new FileWatcher(Paths.get(MenuReader.class.getResource("/").getPath() + "/conf/admin"), new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return StringUtils.equals(name, "systems.xml");
            }
        }, new FileListener() {
            @Override
            public void onCreate(Path file) {

            }

            @Override
            public void onModify(Path file) {//发现修改重新加载
                LOGGER.info("监测到systems.xml被修改，重新加载systems.xml");
                read();
            }

            @Override
            public void onDelete(Path file) {
                LOGGER.info("监测到menus.xml被删除");
            }
        });
        watcher.watch();
    }

    private static final class SingleHolder {
        public static final SystemReader instance = new SystemReader();
    }

    public static final SystemReader getInstance() {
        return SingleHolder.instance;
    }

    private void read() {
        Document document = XmlUtils.parse(CONFIG_PATH);
        List<Node> systemNodes = XmlUtils.getChildren(document, "/systems/system");
        List<com.ruoyi.flow.auth.system.System> systems = Lists.newLinkedList();
        com.ruoyi.flow.auth.system.System system = null;
        for (Node systemNode : systemNodes) {
            String systemName = XmlUtils.getAttributeValue(systemNode, "name");
            Node cityNode = XmlUtils.getChild(systemNode, "city");
            String cityCode = XmlUtils.getAttributeValue(cityNode, "code");
            String cityName = XmlUtils.getAttributeValue(cityNode, "name");
            String orgUrl = XmlUtils.getChild(systemNode, "org").valueOf("@url");
            String roleUrl = XmlUtils.getChild(systemNode, "role").valueOf("@url");
            String userUrl = XmlUtils.getChild(systemNode, "user").valueOf("@url");
            system = new com.ruoyi.flow.auth.system.System(systemName, cityName, cityCode, orgUrl, roleUrl, userUrl);
            systems.add(system);
        }
        this.systems = systems;
    }

    public List<com.ruoyi.flow.auth.system.System> getSystems() {
        return systems;
    }
}
